<template>
  <div>
    <el-dialog :title="authType===1?'授权公众号':'授权小程序'" :visible.sync="dialogVisible" width="650px" :close-on-click-modal="false" @close="close">
      <div v-loading="loading" class="qr-code-container flex-y-center">
        <vue-qr v-if="qrCodeUrl" :text="qrCodeUrl" :size="200" class="qrcode-container" />
        <div class="tips">
          扫一扫授权
        </div>
      </div>
      <!--<div slot="footer" class="dialog-footer">-->
      <!--  <el-button size="small" round @click="hide">取 消</el-button>-->
      <!--  <el-button size="small" round type="primary" @click="submit">确 定</el-button>-->
      <!--</div>-->
    </el-dialog>
  </div>
</template>

<script>
import { authQrcode, queryAuth } from '@/api/wechat'

export default {
  data() {
    return {
      // 配置
      resolve: null,
      reject: null,
      title: '授权公众号',
      // 状态
      dialogVisible: null,
      loading: false,
      isStopCheck: true,
      // 数据
      checkNum: 0,
      qrCodeUrl: null,
      authType: 1,
      classifyCode: null
    }
  },
  computed: {},
  watch: {},
  beforeCreate() {
  },
  created() {
  },
  beforeMount() {
  },
  mounted() {
  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {

  },
  destroyed() {
  },
  methods: {
    /**
     * 弹框相关
     */
    open(authType, classifyCode = null) {
      this.authType = authType
      this.classifyCode = classifyCode
      this.getAuthQrcode()
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.isStopCheck = true
      this.checkNum = 0
      this.qrCodeUrl = null
      this.authType = 1
      this.classifyCode = null
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
     * 数据组
     */
    // 获取二维码
    getAuthQrcode() {
      const params = { 'authType': this.authType }
      if (this.classifyCode) params.classifyCode = this.classifyCode
      this.loading = true
      authQrcode({ data: params }).then(res => {
        this.qrCodeUrl = res.data.url
        this.isStopCheck = false
        this.checkQrcodeState(res.data.codeNo)
      }).finally(() => {
        this.loading = false
      })
    },

    /**
     * 按钮组
     */

    /**
     * 状态组
     */
    // 检查二维码状态
    checkQrcodeState(codeNo) {
      queryAuth({ params: { codeNo } }).then(res => {
        if (this.checkNum >= 300) {
          this.$alert('二维码已过期', '温馨提示', {
            confirmButtonText: '确定',
            center: true,
            type: 'warning',
            callback: () => {
              this.resolve && this.resolve()
              this.hide()
            }
          })
          return
        }
        if (res.data.state === 0) {
          if (!this.isStopCheck) {
            this.checkNum = this.checkNum + 1
            const timerId = setTimeout(() => {
              clearTimeout(timerId)
              this.checkQrcodeState(codeNo)
            }, 2000)
          }
        } else if (res.data.state === 1) {
          this.$alert('授权成功', '温馨提示', {
            confirmButtonText: '确定',
            center: true,
            roundButton: true,
            type: 'success',
            callback: () => {
              this.resolve && this.resolve()
              this.hide()
            }
          })
        } else {
          this.$alert('授权失败：' + res.data.reason, '温馨提示', {
            confirmButtonText: '确定',
            center: true,
            roundButton: true,
            type: 'warning',
            callback: () => {
              this.resolve && this.resolve()
              this.hide()
            }
          })
        }
      })
    }

    /**
     * 方法组
     */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.qrcode-container {
  margin: 30px;
  border: 1px solid #eee;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.tips {
  text-align: center;
  line-height: 1.5;
}
</style>
